package com.online_exam.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.online_exam.domain.ExamPaperQuestionCustomerAnswer;
import com.online_exam.domain.TextContent;
import com.online_exam.domain.enums.QuestionTypeEnum;
import com.online_exam.repository.QuestionAnswerMapper;
import com.online_exam.service.QuestionAnswerService;
import com.online_exam.service.TextContentService;
import com.online_exam.viewmodel.student.exampaper.ExamPaperSubmitItemVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionAnswerServiceImpl implements QuestionAnswerService {
    @Autowired
    QuestionAnswerMapper questionAnswerMapper;
    @Autowired
    TextContentService textContentService;
    
    @Override
    public int insert(ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer) {
        return questionAnswerMapper.insert(examPaperQuestionCustomerAnswer);
    }

    @Override
    public List<ExamPaperQuestionCustomerAnswer> selectListByPaperAnswerId(Integer id) {
        return questionAnswerMapper.selectListByPaperAnswerId(id);
    }

    @Override
    public ExamPaperSubmitItemVM questionAnswersToVM(ExamPaperQuestionCustomerAnswer qa) {
        ExamPaperSubmitItemVM examPaperSubmitItemVM = new ExamPaperSubmitItemVM();
        examPaperSubmitItemVM.setId(qa.getId());
        examPaperSubmitItemVM.setQuestionId(qa.getQuestionId());
        examPaperSubmitItemVM.setDoRight(qa.getDoRight());
        examPaperSubmitItemVM.setItemOrder(qa.getItemOrder());
        examPaperSubmitItemVM.setQuestionScore(qa.getQuestionScore().toString());
        examPaperSubmitItemVM.setScore(qa.getCustomerScore().toString());
        setSpecialToVM(examPaperSubmitItemVM, qa);
        return examPaperSubmitItemVM;
    }

    private void setSpecialToVM(ExamPaperSubmitItemVM examPaperSubmitItemVM, ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer) {
        QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.fromCode(examPaperQuestionCustomerAnswer.getQuestionType());
        switch (questionTypeEnum) {
            case MultipleChoice:
                examPaperSubmitItemVM.setContent(examPaperQuestionCustomerAnswer.getAnswer());
                List list = JSON.parseArray( examPaperQuestionCustomerAnswer.getAnswer());
                System.out.println("---------------"+list);
                examPaperSubmitItemVM.setContentArray(list);
                break;
            case GapFilling:
                //TextContent textContent = textContentService.selectById(examPaperQuestionCustomerAnswer.getTextContentId());
                //List correctAnswer = JSON.parseArray(textContent.getContent());
                List correctAnswer = JSON.parseArray(examPaperQuestionCustomerAnswer.getAnswer());
                examPaperSubmitItemVM.setContentArray(correctAnswer);
                break;
            default:
                if (QuestionTypeEnum.needSaveTextContent(examPaperQuestionCustomerAnswer.getQuestionType())) {
                    TextContent content = textContentService.selectById(examPaperQuestionCustomerAnswer.getTextContentId());
                    examPaperSubmitItemVM.setContent(content.getContent());
                } else {
                    examPaperSubmitItemVM.setContent(examPaperQuestionCustomerAnswer.getAnswer());
                }
                break;
        }
    }
}
